package com.biz.primus.model.order.exception.utils;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.base.exception.ExceptionType;
import com.biz.primus.commodity.exception.CommodityExceptionType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 断言工具，不满足条件抛出则抛出BizSilentException(10002,参数格式校验失败)<br>
 * 推荐使用制定提示语方法，即优先使用有msg参数的方法
 *
 * @author Tiger Feng
 * @date 2017年10月30日
 */
public class AssertRequestParameter {
    private final static ExceptionType REQUEST_PARAM_EXCEPTION = CommodityExceptionType.REQUEST_PARAM_EXCEPTION;

    public static void notNull(Object object, String msg) throws BizSilentException {
        if (object == null) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void notNull(Object object) throws BizSilentException {
        if (object == null) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void isNull(Object object, String msg) throws BizSilentException {
        if (object != null) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void isNull(Object object) throws BizSilentException {
        if (object != null) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void isTrue(boolean expression, String msg) throws BizSilentException {
        if (!expression) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void isTrue(boolean expression) throws BizSilentException {
        if (!expression) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void hasLength(String text, String msg) throws BizSilentException {
        if (!StringUtils.hasLength(text)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void hasLength(String text) throws BizSilentException {
        if (!StringUtils.hasLength(text)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void hasText(String text, String msg) throws BizSilentException {
        if (!StringUtils.hasText(text)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void hasText(String text) throws BizSilentException {
        if (!StringUtils.hasText(text)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void notEmpty(Object[] array, String msg) throws BizSilentException {
        if (ObjectUtils.isEmpty(array)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void notEmpty(Object[] array) throws BizSilentException {
        if (ObjectUtils.isEmpty(array)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void notEmpty(Collection<?> collection, String msg) throws BizSilentException {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void notEmpty(Collection<?> collection) throws BizSilentException {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

    public static void notEmpty(Map<?, ?> map, String msg) throws BizSilentException {
        if (CollectionUtils.isEmpty(map)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION, REQUEST_PARAM_EXCEPTION.getDescription() + ":" + msg);
        }
    }

    public static void notEmpty(Map<?, ?> map) throws BizSilentException {
        if (CollectionUtils.isEmpty(map)) {
            throw new BizSilentException(REQUEST_PARAM_EXCEPTION);
        }
    }

}
